package in.muton.edushare.registration;

import in.muton.edushare.EduSharePage;
import in.muton.edushare.EduShareSession;
import in.muton.edushare.url.URLLoader;

import org.apache.log4j.Logger;
import org.apache.wicket.PageParameters;

import com.edushare.common.constants.URLConstants;
import com.edushare.common.dao.UsersDAO;
import com.edushare.common.util.StringUtil;
import com.edushare.common.vo.Users;

public class VerifyEmailInvitePage extends EduSharePage {

	private static Logger log = Logger.getLogger(VerifyEmailInvitePage.class);
	
	public VerifyEmailInvitePage(PageParameters p) {
		getSession().bind();
		String key = p.getString("key");
		boolean isAuthentic = false;
		Users u;
		
		if(StringUtil.isValid(key)){
			u = new UsersDAO(getSessionORMInterface()).validateEmailInvite(key);
			if(u!=null){
				//set as logged-in user (might be a conflicting user!)
				EduShareSession.get().logInUser(u);
				isAuthentic = true;
			}
		}else{
			setResponsePage(URLLoader.getPageClass(URLConstants.PAGE_NOT_FOUND_PATH));
			return;
		}
		
		log.debug("Verify: " + isAuthentic+", user: "+u);
		
		
			
		if(isAuthentic){
			if(u!=null && u.getStatus()==Users.INVITED) //not using EduShareSession.get().getLoggedInUser() here, because may be this is conflicting user!
			{
				log.debug("user is invited and logged in");
				setResponsePage(InvitedUserLoginCreationPage.class);
				return;
			}
			else
			{
				log.debug("else-1");
				setResponsePage(URLLoader.getPageClass(URLConstants.LOGIN_PATH));
				return;
			}
		}
		else
		{
			log.debug("else-2");
			setResponsePage(URLLoader.getPageClass(URLConstants.LOGIN_PATH));
			return;
		}
		
	}

}
